package com.dong.demo222.controller;

import com.dong.demo222.domin.SysDept;
import com.dong.demo222.domin.SysMenu;
import com.dong.demo222.domin.SysUser;
import com.dong.demo222.domin.TableDataInfo;
import com.dong.demo222.model.AjaxResult;
import com.dong.demo222.service.IPermissionService;
import com.dong.demo222.service.ISysDeptService;
import com.dong.demo222.service.ISysMenuService;
import com.dong.demo222.service.impl.SysMenuServiceImpl;
import com.dong.demo222.service.impl.SysUserServiceImpl;
import com.dong.demo222.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static com.dong.demo222.utils.PageUtils.startPage;

@RestController
public class sysUserController extends BaseController{

    @Autowired
    SysUserServiceImpl sysUserServiceImpl;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysDeptService deptService;

    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysUser sysUser = sysUserServiceImpl.selectUserByUsername(user.getUsername());
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", sysUser);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @RequestMapping("/getUserName")
    public String login(){
        SysUser admin = sysUserServiceImpl.selectUserByUsername("admin");
        return admin.toString();
    }


    //SecurityUtils
    @GetMapping("/getRouters")
    public AjaxResult getRouters()
    {
        User loginUser = SecurityUtils.getLoginUser();
        SysUser admin = sysUserServiceImpl.selectUserByUsername(loginUser.getUsername());

        List<SysMenu> menus = menuService.selectMenuTreeByUserId(admin.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException
    {
        return AjaxResult.success().put("img", "img111111111").put("captchaEnabled",false).put("uuid","0965e7f14f4c4db3825cc587493c3209");
    }

    @GetMapping("/system/user/list")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = sysUserServiceImpl.selectUserList(user);
        return getDataTable(list);
    }

    @GetMapping("/system/user/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }



}
